/*
 * Copyright 2007-2009 Thomas Krause (thomas@berlin-reloaded.de)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gidoo.cms.admin;

import javax.swing.tree.DefaultMutableTreeNode;
import org.gidoo.cms.GidooCMS;
import javax.swing.tree.TreeNode;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.markup.html.tree.BaseTree;
import org.apache.wicket.markup.html.tree.LabelIconPanel;
import org.apache.wicket.markup.html.tree.LinkIconPanel;
import org.apache.wicket.model.IModel;
import org.gidoo.cms.IAdminPlugin;

/**
 * A own implementation of a the subpanel of a tree.
 * @author <a href="mailto:thomas@berlin-reloaded.de">Thomas Krause</a>
 */
public class TreePanel extends LinkIconPanel
{

  private TreeNode usersNode;
  private TreeNode sectionsNode;
  private TreeNode backupNode;

  /** Creates a new instance of TreePanel */
  public TreePanel(String id, IModel model, BaseTree tree,
    TreeNode usersNode, TreeNode sectionsNode, TreeNode backupNode)
  {

    super(id, model, tree);
    this.usersNode = usersNode;
    this.sectionsNode = sectionsNode;
    this.backupNode = backupNode;

  }

  @Override
  protected ResourceReference getResourceFolderClosed(Object node)
  {
    return getRes((TreeNode) node);
  }

  @Override
  protected ResourceReference getResourceFolderOpen(Object node)
  {
    return getRes((TreeNode) node);
  }

  @Override
  protected ResourceReference getResourceItemLeaf(Object node)
  {
    return getRes((TreeNode) node);
  }

  private ResourceReference getRes(TreeNode node)
  {
    DefaultMutableTreeNode nodeAsMutable =
      node instanceof DefaultMutableTreeNode ? (DefaultMutableTreeNode) node : null;

    if(node == usersNode)
    {
      return new ResourceReference(GidooCMS.class, "res/usergroup.png");
    }
    else if(node.getParent() == usersNode)
    {
      return new ResourceReference(GidooCMS.class, "res/user.png");
    }
    else if(node == backupNode)
    {
      return new ResourceReference(GidooCMS.class, "res/importexport.png");
    }
    else if(node == sectionsNode)
    {
      return new ResourceReference(GidooCMS.class, "res/sections.png");
    }
    else if(node.getParent() == sectionsNode)
    {
      return new ResourceReference(GidooCMS.class, "res/singlesection.png");
    }
    else if(nodeAsMutable != null && nodeAsMutable.getUserObject() instanceof IAdminPlugin)
    {
      IAdminPlugin a = (IAdminPlugin) nodeAsMutable.getUserObject();
      return a.getAddinIcon();
    }
    else
    {
      return new ResourceReference(LabelIconPanel.class, "res/item.gif");
    }
  }
}

